package com.foreknow.dao.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Guestbook;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GuestbookDaoImpl extends BaseDao implements GuestbookDao {
    @Override
    public Guestbook login(String name, String pass) {
        //SQL
        //通过调用Jdbc工具类中的方法实现对数据库的操作
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jt.query("select * from guestbook where name=? and pass=?",mapping,name,pass);
        if(list.size() > 0) {
        return (Guestbook) list.get(0);
    }
        return null;
}

    @Override
    public boolean register(Guestbook guestbook) throws SQLException {
        //调用模板类中的update方法
        int i = jt.update("insert into guestbook(name,pass,phone,email,title,content,createdtime) values(?,?,?,?,?,?,?)",
                guestbook.getName(),guestbook.getPass(),guestbook.getPhone(),guestbook.getEmail(),guestbook.getTitle(),guestbook.getContent(),guestbook.getCreatedtime());
        return i == 1;
    }

    public static void main(String[] args) throws SQLException {
            GuestbookDao dao = new GuestbookDaoImpl();
            //        Guestbook gb = dao.login("fender111","1234561111");
            //        if(gb != null) {
            //            System.out.println("success......");
            //            System.out.println(gb.getName() + "---" + gb.getPass());
            //        }else {
            //            System.out.println("No Data......");
            //        }
            Guestbook gb = new Guestbook();
            gb.setName("xxx");
            gb.setPass("111111");
            gb.setPhone("12121212121");
        gb.setEmail("xxx@163.com");
        boolean isRight = dao.register(gb);
        if(isRight) {
            System.out.println("add success");
        }else {
            System.out.println("add error");
        }

    }

    @Override
    public Guestbook getQueryById(int id) {
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jt.query("select * from guestbook where id=?",mapping,id);
        if(list.size() > 0) {
            return (Guestbook) list.get(0);
        }
        return null;
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        int i = jt.update("delete from guestbook where id=?",id);
        return i==1;
    }


    @Override
    public List<Guestbook> getAll() {
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jt.query("select * from guestbook",mapping);
        List<Guestbook> newList = new ArrayList<>(); //创建新的集合并遍历原有集合并将原集合中的对象添加到新的集合中
        for (int i = 0;i<list.size();i++) {
            Guestbook gb = (Guestbook) list.get(i);
            newList.add(gb);
        }
        return newList;
    }

    @Override
    public boolean update(Guestbook gb) {
        int i = jt.update("update guestbook set name=?,pass=?,phone=?,email=?,title=?,content=?,createdtime=? where id=?"
                ,gb.getName(),gb.getPass(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent(),gb.getCreatedtime(),gb.getId());
        return i == 1;
    }
}
